home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / man / cat.n / expr.n < prev    next >
Text File  |  1995-07-25  |  19KB  |  390 lines

  1.  
  2.  
  3.  
  4.      eeeexxxxpppprrrr((((nnnn))))                     TTTTccccllll ((((7777....0000))))                     eeeexxxxpppprrrr((((nnnn))))
  5.  
  6.  
  7.  
  8.      _________________________________________________________________
  9.  
  10.      NNNNAAAAMMMMEEEE
  11.           expr - Evalue an expression
  12.  
  13.      SSSSYYYYNNNNOOOOPPPPSSSSIIIISSSS
  14.           eeeexxxxpppprrrr _a_r_g ?_a_r_g _a_r_g ...?
  15.      _________________________________________________________________
  16.  
  17.  
  18.      DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
  19.           Concatenates _a_r_g's (adding separator spaces  between  them),  |
  20.           evaluates  the  result  as a Tcl expression, and returns the  |
  21.           value.  The operators permitted in  Tcl  expressions  are  a
  22.           subset of the operators permitted in C expressions, and they
  23.           have the same meaning and precedence as the corresponding  C
  24.           operators.   Expressions almost always yield numeric results
  25.           (integer  or  floating-point  values).   For  example,   the
  26.           expression
  27.  
  28.                eeeexxxxpppprrrr 8888....2222 ++++ 6666
  29.           evaluates  to  14.2.   Tcl   expressions   differ   from   C
  30.           expressions  in  the way that operands are specified.  Also,
  31.           Tcl expressions  support  non-numeric  operands  and  string
  32.           comparisons.
  33.  
  34.      OOOOPPPPEEEERRRRAAAANNNNDDDDSSSS
  35.           A Tcl expression consists  of  a  combination  of  operands,
  36.           operators, and parentheses.  White space may be used between
  37.           the operands and operators and parentheses; it is ignored by
  38.           the  expression  processor.   Where  possible,  operands are
  39.           interpreted  as  integer  values.   Integer  values  may  be
  40.           specified  in  decimal  (the  normal case), in octal (if the
  41.           first character of the operand is 0000), or in hexadecimal  (if
  42.           the  first  two  characters  of  the operand are 0000xxxx).  If an
  43.           operand does not have  one  of  the  integer  formats  given
  44.           above, then it is treated as a floating-point number if that
  45.           is possible.  Floating-point numbers may be specified in any
  46.           of the ways accepted by an ANSI-compliant C compiler (except
  47.           that the ``f'', ``F'', ``l'', and ``L'' suffixes will not be
  48.           permitted  in  most installations).  For example, all of the
  49.           following are valid floating-point numbers:  2.1,  3.,  6e4,
  50.           7.91e+16.  If no numeric interpretation is possible, then an
  51.           operand is left as a string  (and  only  a  limited  set  of
  52.           operators may be applied to it).
  53.  
  54.           Operands may be specified in any of the following ways:
  55.  
  56.           [1]  As an numeric value, either integer or floating-point.
  57.  
  58.           [2]  As a Tcl variable,  using  standard  $$$$  notation.   The
  59.                variable's value will be used as the operand.
  60.  
  61.  
  62.      Page 1                                          (printed 7/17/95)
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.      eeeexxxxpppprrrr((((nnnn))))                     TTTTccccllll ((((7777....0000))))                     eeeexxxxpppprrrr((((nnnn))))
  70.  
  71.  
  72.  
  73.           [3]  As a string enclosed in double-quotes.  The  expression
  74.                parser  will  perform  backslash, variable, and command
  75.                substitutions on the information  between  the  quotes,
  76.                and use the resulting value as the operand
  77.  
  78.           [4]  As a string enclosed in braces.  The characters between
  79.                the open brace and matching close brace will be used as
  80.                the operand without any substitutions.
  81.  
  82.           [5]  As a Tcl command enclosed  in  brackets.   The  command
  83.                will  be  executed  and  its result will be used as the
  84.                operand.
  85.  
  86.           [6]  As a mathematical function whose arguments have any  of  |
  87.                the above forms for operands, such as ``ssssiiiinnnn(((($$$$xxxx))))''.  See  |
  88.                below for a list of defined functions.
  89.  
  90.           Where  substitutions  occur  above   (e.g.   inside   quoted
  91.           strings),  they  are  performed by the expression processor.
  92.           However, an additional layer  of  substitution  may  already
  93.           have  been  performed  by  the  command  parser  before  the
  94.           expression processor was called.  As discussed below, it  is
  95.           usually best to enclose expressions in braces to prevent the
  96.           command  parser  from  performing   substitutions   on   the
  97.           contents.
  98.  
  99.           For  some  examples  of  simple  expressions,  suppose   the
  100.           variable  aaaa has the value 3 and the variable bbbb has the value
  101.           6.  Then the command on the left side of each of  the  lines
  102.           below will produce the value on the right side of the line:
  103.  
  104.                eeeexxxxpppprrrr 3333....1111 ++++ $$$$aaaa           6666....1111
  105.                eeeexxxxpppprrrr 2222 ++++ """"$$$$aaaa....$$$$bbbb""""        5555....6666
  106.                eeeexxxxpppprrrr 4444****[[[[lllllllleeeennnnggggtttthhhh """"6666 2222""""]]]]  8888
  107.                eeeexxxxpppprrrr {{{{{{{{wwwwoooorrrrdddd oooonnnneeee}}}} <<<< """"wwwwoooorrrrdddd $$$$aaaa""""}}}}0000
  108.      OOOOPPPPEEEERRRRAAAATTTTOOOORRRRSSSS
  109.           The valid operators are listed below, grouped in  decreasing
  110.           order of precedence:
  111.  
  112.           ----  ~~~~  !!!!             Unary minus, bit-wise NOT, logical  NOT.
  113.                               None of these operands may be applied to
  114.                               string operands, and bit-wise NOT may be
  115.                               applied only to integers.
  116.  
  117.           ****  ////  %%%%             Multiply, divide,  remainder.   None  of
  118.                               these  operands may be applied to string
  119.                               operands, and remainder may  be  applied
  120.                               only  to  integers.   The remainder will  |
  121.                               always have the same sign as the divisor  |
  122.                               and  an  absolute value smaller than the  |
  123.                               divisor.
  124.  
  125.  
  126.      Page 2                                          (printed 7/17/95)
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.      eeeexxxxpppprrrr((((nnnn))))                     TTTTccccllll ((((7777....0000))))                     eeeexxxxpppprrrr((((nnnn))))
  134.  
  135.  
  136.  
  137.           ++++  ----                Add and subtract.  Valid for any numeric
  138.                               operands.
  139.  
  140.           <<<<<<<<  >>>>>>>>              Left and right shift.  Valid for integer
  141.                               operands only.
  142.  
  143.           <<<<  >>>>  <<<<====  >>>>====        Boolean  less,  greater,  less  than  or
  144.                               equal,  and greater than or equal.  Each
  145.                               operator produces 1 if the condition  is
  146.                               true,  0 otherwise.  These operators may
  147.                               be applied to strings as well as numeric
  148.                               operands,    in    which   case   string
  149.                               comparison is used.
  150.  
  151.           ========  !!!!====              Boolean  equal  and  not  equal.    Each
  152.                               operator  produces  a  zero/one  result.
  153.                               Valid for all operand types.
  154.  
  155.           &&&&                   Bit-wise   AND.    Valid   for   integer
  156.                               operands only.
  157.  
  158.           ^^^^                   Bit-wise  exclusive   OR.    Valid   for
  159.                               integer operands only.
  160.  
  161.           ||||                   Bit-wise OR.  Valid for integer operands
  162.                               only.
  163.  
  164.           &&&&&&&&                  Logical AND.  Produces  a  1  result  if
  165.                               both operands are non-zero, 0 otherwise.
  166.                               Valid   for   numeric   operands    only
  167.                               (integers or floating-point).
  168.  
  169.           ||||||||                  Logical OR.  Produces a 0 result if both
  170.                               operands  are  zero, 1 otherwise.  Valid
  171.                               for numeric operands only  (integers  or
  172.                               floating-point).
  173.  
  174.           _x????_y::::_z               If-then-else, as in C.  If  _x  evaluates
  175.                               to  non-zero,  then  the  result  is the
  176.                               value of _y.  Otherwise the result is the
  177.                               value  of  _z.  The _x operand must have a
  178.                               numeric value.
  179.  
  180.           See the C manual for more details on the results produced by
  181.           each  operator.   All of the binary operators group left-to-
  182.           right within the same precedence level.   For  example,  the
  183.           command
  184.  
  185.                eeeexxxxpppprrrr 4444****2222 <<<< 7777
  186.           returns 0.
  187.  
  188.  
  189.  
  190.      Page 3                                          (printed 7/17/95)
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.      eeeexxxxpppprrrr((((nnnn))))                     TTTTccccllll ((((7777....0000))))                     eeeexxxxpppprrrr((((nnnn))))
  198.  
  199.  
  200.  
  201.           The &&&&&&&&, ||||||||, and ????:::: operators have ``lazy evaluation'',  just
  202.           as in C, which means that operands are not evaluated if they
  203.           are not needed to determine the outcome.   For  example,  in
  204.           the command
  205.  
  206.                eeeexxxxpppprrrr {{{{$$$$vvvv ???? [[[[aaaa]]]] :::: [[[[bbbb]]]]}}}}
  207.           only one of [[[[aaaa]]]] or [[[[bbbb]]]] will actually be evaluated, depending
  208.           on  the  value of $$$$vvvv.  Note, however, that this is only true
  209.           if the entire expression is enclosed in  braces;   otherwise
  210.           the  Tcl  parser  will  evaluate  both  [[[[aaaa]]]]  and  [[[[bbbb]]]] before
  211.           invoking the eeeexxxxpppprrrr command.
  212.  
  213.      MMMMAAAATTTTHHHH FFFFUUUUNNNNCCCCTTTTIIIIOOOONNNNSSSS
  214.           Tcl  supports  the  following  mathematical   functions   in  |
  215.           expressions:                                                  |
  216.  
  217.                aaaaccccoooossss        ccccoooossss         hhhhyyyyppppooootttt      ssssiiiinnnnhhhh                  |
  218.                aaaassssiiiinnnn        ccccoooosssshhhh        lllloooogggg        ssssqqqqrrrrtttt                  |
  219.                aaaattttaaaannnn        eeeexxxxpppp         lllloooogggg11110000      ttttaaaannnn                   |
  220.                aaaattttaaaannnn2222       fffflllloooooooorrrr       ppppoooowwww        ttttaaaannnnhhhh                  |
  221.                cccceeeeiiiillll        ffffmmmmoooodddd        ssssiiiinnnn                              |
  222.           Each of these functions invokes the math library function of  |
  223.           the  same  name;   see  the  manual  entries for the library  |
  224.           functions for details on what they do.  Tcl also  implements  |
  225.           the  following functions for conversion between integers and  |
  226.           floating-point numbers:                                       |
  227.  
  228.           aaaabbbbssss((((_a_r_g))))                                                           ||
  229.                Returns  the  absolute value of _a_r_g.  _A_r_g may be either  |
  230.                integer or floating-point, and the result  is  returned  |
  231.                in the same form.                                        |
  232.  
  233.           ddddoooouuuubbbblllleeee((((_a_r_g))))                                                        ||
  234.                If  _a_r_g  is  a  floating  value, returns _a_r_g, otherwise  |
  235.                converts _a_r_g to  floating  and  returns  the  converted  |
  236.                value.                                                   |
  237.  
  238.           iiiinnnntttt((((_a_r_g))))                                                           ||
  239.                If  _a_r_g  is  an  integer  value, returns _a_r_g, otherwise  |
  240.                converts _a_r_g to integer by truncation and  returns  the  |
  241.                converted value.                                         |
  242.  
  243.           rrrroooouuuunnnndddd((((_a_r_g))))                                                         ||
  244.                If  _a_r_g  is  an  integer  value, returns _a_r_g, otherwise  |
  245.                converts _a_r_g to integer by  rounding  and  returns  the  |
  246.                converted value.                                         |
  247.  
  248.           In addition to these predifined functions, applications  may  |
  249.           define additional functions using TTTTccccllll____CCCCrrrreeeeaaaatttteeeeMMMMaaaatttthhhhFFFFuuuunnnncccc().
  250.  
  251.      TTTTYYYYPPPPEEEESSSS,,,, OOOOVVVVEEEERRRRFFFFLLLLOOOOWWWW,,,, AAAANNNNDDDD PPPPRRRREEEECCCCIIIISSSSIIIIOOOONNNN
  252.  
  253.  
  254.  
  255.      PPPPaaaaggggeeee 4444                                          ((((pppprrrriiiinnnntttteeeedddd 7777////11117777////99995555))))
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.      eeeexxxxpppprrrr((((nnnn))))                     TTTTccccllll ((((7777....0000))))                     eeeexxxxpppprrrr((((nnnn))))
  263.  
  264.  
  265.  
  266.           All internal computations involving integers are  done  with
  267.           the  C  type  _l_o_n_g,  and all internal computations involving
  268.           floating-point are  done  with  the  C  type  _d_o_u_b_l_e.   When
  269.           converting  a string to floating-point, exponent overflow is
  270.           detected and results in a  Tcl  error.   For  conversion  to
  271.           integer  from  string,  detection of overflow depends on the
  272.           behavior of some routines in the  local  C  library,  so  it
  273.           should  be  regarded  as  unreliable.   In any case, integer
  274.           overflow and underflow are generally not  detected  reliably
  275.           for   intermediate  results.   Floating-point  overflow  and
  276.           underflow are  detected  to  the  degree  supported  by  the
  277.           hardware, which is generally pretty reliable.
  278.  
  279.           Conversion  among  internal  representations  for   integer,
  280.           floating-point, and string operands is done automatically as
  281.           needed.  For  arithmetic  computations,  integers  are  used
  282.           until  some floating-point number is introduced, after which
  283.           floating-point is used.  For example,
  284.  
  285.                eeeexxxxpppprrrr 5555 //// 4444
  286.           returns 1, while
  287.  
  288.                eeeexxxxpppprrrr 5555 //// 4444....0000
  289.                eeeexxxxpppprrrr 5555 //// (((( [[[[ssssttttrrrriiiinnnngggg lllleeeennnnggggtttthhhh """"aaaabbbbccccdddd""""]]]] ++++ 0000....0000 ))))
  290.           both return 1.25.  Floating-point values are always returned  |
  291.           with  a  ``.''  or  an ``e'' so that they will not look like  |
  292.           integer values.  For example,                                 |
  293.  
  294.                eeeexxxxpppprrrr 22220000....0000////5555....0000                                            |
  295.           returns   ``4.0'',   not   ``4''.    The   global   variable  |
  296.           ttttccccllll____pppprrrreeeecccciiiissssiiiioooonnnn  determines  the  the  number  of  significant  |
  297.           digits that are retained when floating values are  converted  |
  298.           to  strings  (except  that trailing zeroes are omitted).  If  |
  299.           ttttccccllll____pppprrrreeeecccciiiissssiiiioooonnnn is unset then 6 digits of precision are  used.  |
  300.           To  retain  all of the significant bits of an IEEE floating-  |
  301.           point number  set  ttttccccllll____pppprrrreeeecccciiiissssiiiioooonnnn  to  17;   if  a  value  is  |
  302.           converted  to  string  with  17 digits of precision and then  |
  303.           converted back to binary for  some  later  calculation,  the  |
  304.           resulting  binary value is guaranteed to be identical to the  |
  305.           original one.
  306.  
  307.  
  308.      SSSSTTTTRRRRIIIINNNNGGGG OOOOPPPPEEEERRRRAAAATTTTIIIIOOOONNNNSSSS
  309.           String values may be used  as  operands  of  the  comparison
  310.           operators,  although  the  expression  evaluator tries to do
  311.           comparisons as integer or floating-point when  it  can.   If
  312.           one  of  the  operands  of  a comparison is a string and the
  313.           other has a numeric value, the numeric operand is  converted
  314.           back to a string using the C _s_p_r_i_n_t_f format specifier %%%%dddd for
  315.           integers and %%%%gggg for floating-point values.  For example, the
  316.           commands
  317.  
  318.  
  319.      Page 5                                          (printed 7/17/95)
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.      eeeexxxxpppprrrr((((nnnn))))                     TTTTccccllll ((((7777....0000))))                     eeeexxxxpppprrrr((((nnnn))))
  327.  
  328.  
  329.  
  330.                eeeexxxxpppprrrr {{{{""""0000xxxx00003333"""" >>>> """"2222""""}}}}
  331.                eeeexxxxpppprrrr {{{{""""0000yyyy"""" <<<< """"0000xxxx11112222""""}}}}
  332.           both return 1.  The first comparison is done  using  integer
  333.           comparison,  and  the second is done using string comparison
  334.           after the second operand is converted to the string ``18''.
  335.  
  336.  
  337.      KKKKEEEEYYYYWWWWOOOORRRRDDDDSSSS
  338.           arithmetic, boolean, compare, expression
  339.  
  340.  
  341.  
  342.  
  343.  
  344.  
  345.  
  346.  
  347.  
  348.  
  349.  
  350.  
  351.  
  352.  
  353.  
  354.  
  355.  
  356.  
  357.  
  358.  
  359.  
  360.  
  361.  
  362.  
  363.  
  364.  
  365.  
  366.  
  367.  
  368.  
  369.  
  370.  
  371.  
  372.  
  373.  
  374.  
  375.  
  376.  
  377.  
  378.  
  379.  
  380.  
  381.  
  382.  
  383.      Page 6                                          (printed 7/17/95)
  384.  
  385.  
  386.  
  387.